#!/bin/bash

# SJBlog at Tomcat7 with Docker 部署脚本
echo ==================================================
echo Script startup for deploy SJBlog at Tomcat7 with Docker
echo ==================================================

# Tomcat7镜像库地址
TOMCAT7_DOCKER_REGISTRY=daocloud.io/library/tomcat
# Tomcat7镜像版本
TOMCAT7_DOCKER_VERSION=7.0.57-jre7
# Tomcat7的Docker实例名称
TOMCAT7_DOCKER_INSTANCE_NAME=tomcat7-stevejrongblog
# Tomcat7物理机HTTP映射端口
TOMCAT7_PHYSICAL_MACHINE_HTTP_PORT=520
# Tomcat7虚拟机HTTP映射端口
TOMCAT7_VIRTUAL_MACHINE_HTTP_PORT=520
# Tomcat7物理机中web-apps路径
TOMCAT7_PHYSICAL_MACHINE_WEB_APPS_PATH=/home/stevejrong-blog-web-app/web-app
# Tomcat7虚拟机中web-apps路径
TOMCAT7_VIRTUAL_MACHINE_WEB_APPS_PATH=/usr/local/tomcat/webapps
# Tomcat7物理机中Tomcat配置文件路径
TOMCAT7_PHYSICAL_MACHINE_CONF_PATH=/home/stevejrong-blog-web-app/docker-tomcat-conf
# Tomcat7虚拟机中Tomcat配置文件路径
TOMCAT7_VIRTUAL_MACHINE_CONF_PATH=/usr/local/tomcat/conf
# Tomcat7物理机中Tomcat配置文件路径
TOMCAT7_PHYSICAL_MACHINE_SSL_CERTS_PATH=/home/documents/ssl_certificates
# Tomcat7虚拟机中Tomcat配置文件路径
TOMCAT7_VIRTUAL_MACHINE_SSL_CERTS_PATH=/usr/local/tomcat/certs

############################ installing start ############################
echo -e "-------------------- installing\n"

echo "-------------------- pulling tomcat7 docker image"
docker pull $TOMCAT7_DOCKER_REGISTRY:$TOMCAT7_DOCKER_VERSION
echo -e "-------------------- pull success.\n"
sleep 3

echo "-------------------- running SJBlog at tomcat7"
docker run -d -p $TOMCAT7_PHYSICAL_MACHINE_HTTP_PORT:$TOMCAT7_VIRTUAL_MACHINE_HTTP_PORT -v $TOMCAT7_PHYSICAL_MACHINE_WEB_APPS_PATH:$TOMCAT7_VIRTUAL_MACHINE_WEB_APPS_PATH:rw -v $TOMCAT7_PHYSICAL_MACHINE_CONF_PATH:$TOMCAT7_VIRTUAL_MACHINE_CONF_PATH:rw -v $TOMCAT7_PHYSICAL_MACHINE_SSL_CERTS_PATH:$TOMCAT7_VIRTUAL_MACHINE_SSL_CERTS_PATH:rw --name $TOMCAT7_DOCKER_INSTANCE_NAME --restart always --privileged=true $TOMCAT7_DOCKER_REGISTRY:$TOMCAT7_DOCKER_VERSION
echo -e "-------------------- run success.\n"
sleep 3

echo -e "-------------------- install completed!\n"
############################ installing end ############################

############################ validating start ############################
echo -e "-------------------- validating\n"

echo -e "-------------------- validating tomcat7 run status\n"
docker ps -a | grep "$TOMCAT7_DOCKER_INSTANCE_NAME"
sleep 3

echo -e "-------------------- validate completed!\n"
############################ validating end ############################

echo =================================================
echo Deploy SJBlog at Tomcat7 with Docker completed!
echo =================================================